package com.beeantec.jnrerc.modules.course.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.beeantec.jnrerc.core.enums.CategoryTypeEnum;
import com.beeantec.jnrerc.core.enums.StatusIdEnum;
import com.beeantec.jnrerc.dto.CourseCategoryDTO;
import com.beeantec.jnrerc.dto.response.CourseCategoryListDTO;
import com.beeantec.jnrerc.entity.CourseCategory;
import com.beeantec.jnrerc.mapper.CourseCategoryMapper;
import com.beeantec.jnrerc.modules.course.service.CourseCategoryService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;


@Service
public class CourseCategoryServiceImpl extends ServiceImpl<CourseCategoryMapper, CourseCategory> implements CourseCategoryService {

    /**
     * 获取课程分类列表
     *
     */
    @Override
    public CourseCategoryListDTO listAll() {
        CourseCategoryListDTO dto = new CourseCategoryListDTO();
        // 根据分类类型、层级查询可用状态的课程分类集合
        List<CourseCategory> oneCategoryList = listByCategoryTypeAndFloorAndStatusId(CategoryTypeEnum.COURSE.getCode(), 1, StatusIdEnum.YES.getCode());
        if (StringUtils.isEmpty(oneCategoryList)) {
            return dto;
        }
        List<CourseCategoryDTO> oneList = new ArrayList<>();
        // 查找一级分类下的二级分类
        for (CourseCategory courseCategory : oneCategoryList) {
            CourseCategoryDTO courseCategoryDTO = new CourseCategoryDTO();
            courseCategoryDTO = BeanUtil.copyProperties(courseCategory, CourseCategoryDTO.class);
            // 查找一级分类下的二级分类
            QueryWrapper<CourseCategory> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(CourseCategory::getParentId, courseCategory.getId())
                    .eq(CourseCategory::getFloor,2);
            List<CourseCategory> twoCategoryList = baseMapper.selectList(wrapper);
            oneList.add(courseCategoryDTO);
            dto.setCourseCategoryList(oneList);
            List<CourseCategoryDTO> twoList = new ArrayList<>();
            for (CourseCategory twoCategory : twoCategoryList) {
                CourseCategoryDTO twoDto = new CourseCategoryDTO();
                twoDto = BeanUtil.copyProperties(twoCategory, CourseCategoryDTO.class);
                twoList.add(twoDto);
            }
            courseCategoryDTO.setTwoList(twoList);
        }
        dto.setCourseCategoryList(oneList);
        return dto;
    }

    @Override
    public CourseCategoryListDTO listAllProject() {
        CourseCategoryListDTO dto = new CourseCategoryListDTO();
        // 根据分类类型、层级查询可用状态的课程分类集合
        List<CourseCategory> oneCategoryList = listByCategoryTypeAndFloorAndStatusId(CategoryTypeEnum.PROJECT.getCode(), 1, StatusIdEnum.YES.getCode());
        if (StringUtils.isEmpty(oneCategoryList)) {
            return dto;
        }
        List<CourseCategoryDTO> oneList = new ArrayList<>();
        // 查找一级分类下的二级分类
        for (CourseCategory courseCategory : oneCategoryList) {
            CourseCategoryDTO courseCategoryDTO = new CourseCategoryDTO();
            courseCategoryDTO = BeanUtil.copyProperties(courseCategory, CourseCategoryDTO.class);
            // 查找一级分类下的二级分类
            QueryWrapper<CourseCategory> wrapper = new QueryWrapper<>();
            wrapper.lambda().eq(CourseCategory::getParentId, courseCategory.getId())
                    .eq(CourseCategory::getFloor,2);
            List<CourseCategory> twoCategoryList = baseMapper.selectList(wrapper);
            oneList.add(courseCategoryDTO);
            dto.setCourseCategoryList(oneList);
            List<CourseCategoryDTO> twoList = new ArrayList<>();
            for (CourseCategory twoCategory : twoCategoryList) {
                CourseCategoryDTO twoDto = new CourseCategoryDTO();
                twoDto = BeanUtil.copyProperties(twoCategory, CourseCategoryDTO.class);
                twoList.add(twoDto);
            }
            courseCategoryDTO.setTwoList(twoList);
        }
        dto.setCourseCategoryList(oneList);
        return dto;
    }

    private List<CourseCategory> listByCategoryTypeAndFloorAndStatusId(Integer categoryType, Integer floor, Integer statusId){
        QueryWrapper<CourseCategory> categoryQueryWrapper = new QueryWrapper<>();
        categoryQueryWrapper.lambda().eq(CourseCategory::getCategoryType,categoryType)
                .eq(CourseCategory::getStatusId,statusId)
                .eq(CourseCategory::getFloor,floor)
                .orderByDesc(CourseCategory::getSort);

        return  baseMapper.selectList(categoryQueryWrapper);
    }
}
